<template>
  <div class="teachers_page">
    <div style="display:flex;align-items: center;justify-content: space-between;margin:0 0 10px;">
      <div style="display:flex;align-items: center;">
        <el-form :inline="true" :model="formInline" class="demo-form-inline">
          <el-form-item label="商品名称">
            <el-input clearable size="small" v-model="formInline.name" placeholder="商品名称"></el-input>
          </el-form-item>
          <el-form-item label="状态">
            <el-select clearable size="small" v-model="formInline.state" placeholder="请选择">
              <el-option
                v-for="item in [{
                  value: '1',
                  label: '启用'
                },{
                  value: '0',
                  label: '禁用'
                },]"
                :key="item.value"
                :label="item.label"
                :value="item.value">
              </el-option>
            </el-select>
          </el-form-item>
        </el-form>
        <el-button size="small" type="primary" @click="onSubmit">搜索</el-button>
        <el-button size="small" @click="reset">重置</el-button>
      </div>
      <div>
      <el-button size="small" type="primary" @click="add">新建商品</el-button>

    </div>
  </div>
  <el-table ref="multipleTable" v-loading="listLoading" :data="list" element-loading-text="Loading" border fit highlight-current-row empty-text="暂无数据">
    <el-table-column label="ID" width="100">
      <template slot-scope="scope">
        {{ scope.row.id }}
      </template>
    </el-table-column>
    <el-table-column :show-overflow-tooltip="true" label="商品名称">
      <template slot-scope="scope">
        {{ scope.row.name }}
      </template>
    </el-table-column>
    <el-table-column label="缩略图" width="121">
      <template slot-scope="scope">
        <img :src="scope.row.thumbUrl" style="width:100px;height:100px" alt="">
      </template>
    </el-table-column>
    <el-table-column label="展示排序" width="100">
      <template slot-scope="scope">
        {{ scope.row.sequencing }}
      </template>
    </el-table-column>
    <el-table-column label="积分金额" width="200">
      <template slot-scope="scope">
        {{ scope.row.cost }}
      </template>
    </el-table-column>
    <el-table-column label="商品数量" width="200">
      <template slot-scope="scope">
        {{ scope.row.quantity }}
      </template>
    </el-table-column>
    <el-table-column :show-overflow-tooltip="true" label="商品描述" width="300">
      <template slot-scope="scope">
        {{ scope.row.description }}
      </template>
    </el-table-column>
    <el-table-column label="创建时间" width="200">
      <template slot-scope="scope">
        {{ scope.row.createTime }}
      </template>
    </el-table-column>
    <el-table-column label="状态" width="100">
      <template slot-scope="scope">
        <el-tag style="cursor:pointer;" size="small" v-if="scope.row.state == 0" type="danger">禁用</el-tag>
        <el-tag style="cursor:pointer;" size="small" v-else type="success">启用</el-tag>
      </template>
    </el-table-column>
    <el-table-column fixed="right" label="操作" width="200">
      <template slot-scope="scope">
        <el-button @click="changeState(scope.row, 0)" type="text" style="color:#ff0000;" v-if="scope.row.state == 1">禁用</el-button>
        <el-button @click="changeState(scope.row, 1)" type="text" v-else>启用</el-button>
        <el-divider direction="vertical"></el-divider>
        <el-button @click="edit(scope.row)" type="text">编辑</el-button>
        <el-divider direction="vertical"></el-divider>
        <el-popconfirm
          @onConfirm="del(scope.row)"
          confirm-button-text='确定'
          cancel-button-text='取消'
          icon="el-icon-info"
          icon-color="red"
          title="是否删除本条数据？">
          <el-button  slot="reference" type="text" style="color:#ff0000;">删除</el-button>
        </el-popconfirm>
      </template>
    </el-table-column>
  </el-table>
  <div style="margin:10px 0 0 0;text-align:right;">
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="pageObj.pageNum"
      :page-sizes="[10, 30, 50, 100]"
      :page-size="pageObj.pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="pageObj.total">
    </el-pagination>
  </div>
</div>
</template>

<script>
import {
  getGoodsListApi,
  deleteGoodsApi,
  changeGoodsStateApi,
} from '@/api/repurchase'

export default {
  data() {
    return {
      list: [],
      listLoading: false,
      currentPage: 1,
      formInline: {
        name: '',
        state: '',
      },
      pageObj: {
        pageSize: 10,
        pageNum: 1,
        total: 0,
      },
    }
  },
  created() {
    this.fetchData()
  },
  methods: {
    exportList() {
      window.open(`https://service.visualbook.cn/question/export/1`)
    },
    fileSuccess(e) {
      if(e.code  == 200) {
        this.$message.success(e.data)
        this.fetchData()
      }
    },
    fileChange(e) {
      if(e.status == 'success') {
        if(e.response.code != 200) {
          this.$message.error(e.response.message)
        }
      }
    },
    onSubmit() {
      this.fetchData()
    },
    reset() {
      this.formInline.name = ''
      this.formInline.state = ''
      this.pageObj.pageSize = 10
      this.pageObj.pageNum = 1
      this.fetchData()
    },
    changeState(row, state) {
      changeGoodsStateApi({
        id: row.id,
        state: state
      }).then((res) => {
        this.fetchData()
      })
    },
    del(row) {
      deleteGoodsApi({
        ids: row.id
      }).then((res) => {
        this.fetchData()
      })
    },
    edit(row) {
      console.log(row)
      this.$router.push({
        path: '/repurchase/edit',
        query: {
          editId: row.id
        }
      })
    },
    add() {
      this.$router.push({
        path: '/repurchase/add'
      })
    },
    fetchData() {
      this.listLoading = true
      getGoodsListApi({
        name: this.formInline.name,
        state: this.formInline.state,
        pageNum: this.pageObj.pageNum,
        pageSize: this.pageObj.pageSize,
        source: 'score'
      }).then(response => {
        this.list = response.data.result
        this.listLoading = false
        this.pageObj.total = response.data.total
      })
    },
    handleSizeChange(val) {
      // console.log(`每页 ${val} 条`);
      this.pageObj.pageSize = val
      this.fetchData()
    },
    handleCurrentChange(val) {
      // console.log(`当前页: ${val}`);
      this.pageObj.pageNum = val
      this.fetchData()
    }
  }
}
</script>

<style>
  .el-table .cell{
    white-space: pre-line;
  }
</style>

<style lang="scss" scoped>
   .teachers_page{
     padding: 20px;
     .el-form-item{
       margin-bottom: 0;
     }
   }
</style>
